<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Python连接C实现高效编程demo | 小豆子的Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="一、前言作为一门经典的编程语言，这些年来，C有着重要的地位，在各个领域的使用都是相当重要的地位！而Python作为较新式的语言，相对而言入门更加简单，适合新手来学习！但是作为一门动态的脚本语言，其效率相对C来说还是有些捉急的。在这里，我们引入一个demo来作为Python和C混合编程的一个基础。 二、编写代码在这里我会使用对比的方式来比较两种语言计算一个较大数字求和时的耗时情况，从而直观体验其中的">
<meta property="og:type" content="article">
<meta property="og:title" content="Python连接C实现高效编程demo">
<meta property="og:url" content="http://yoursite.com/2020/05/31/Python%E8%BF%9E%E6%8E%A5C%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%95%88%E7%BC%96%E7%A8%8Bdemo/index.html">
<meta property="og:site_name" content="小豆子的Blog">
<meta property="og:description" content="一、前言作为一门经典的编程语言，这些年来，C有着重要的地位，在各个领域的使用都是相当重要的地位！而Python作为较新式的语言，相对而言入门更加简单，适合新手来学习！但是作为一门动态的脚本语言，其效率相对C来说还是有些捉急的。在这里，我们引入一个demo来作为Python和C混合编程的一个基础。 二、编写代码在这里我会使用对比的方式来比较两种语言计算一个较大数字求和时的耗时情况，从而直观体验其中的">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-05-31T00:54:12.000Z">
<meta property="article:modified_time" content="2020-05-31T01:22:04.261Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="小豆子的Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">小豆子的Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-Python连接C实现高效编程demo" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/05/31/Python%E8%BF%9E%E6%8E%A5C%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%95%88%E7%BC%96%E7%A8%8Bdemo/" class="article-date">
  <time datetime="2020-05-31T00:54:12.000Z" itemprop="datePublished">2020-05-31</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Python连接C实现高效编程demo
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h2><p>作为一门经典的编程语言，这些年来，C有着重要的地位，在各个领域的使用都是相当重要的地位！而Python作为较新式的语言，相对而言入门更加简单，适合新手来学习！但是作为一门动态的脚本语言，其效率相对C来说还是有些捉急的。在这里，我们引入一个demo来作为Python和C混合编程的一个基础。</p>
<h2 id="二、编写代码"><a href="#二、编写代码" class="headerlink" title="二、编写代码"></a>二、编写代码</h2><p>在这里我会使用对比的方式来比较两种语言计算一个较大数字求和时的耗时情况，从而直观体验其中的差别。同时这里会引入混合编程的概念。   </p>
<ol>
<li><p>编写C的代码</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">void add(long VALUE)&#123;</span><br><span class="line">    long sum &#x3D; 0;</span><br><span class="line">    for (long i &#x3D; 1;i &lt; VALUE; i ++)</span><br><span class="line">        sum +&#x3D; i;</span><br><span class="line">    printf(&quot;C求和结果为: %ld\n&quot;, sum);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>编写Python代码</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import time</span><br><span class="line"></span><br><span class="line">VALUE &#x3D; 9999999</span><br><span class="line"></span><br><span class="line">t1 &#x3D; time.time()</span><br><span class="line">sum &#x3D; 0</span><br><span class="line">for i in range(1, VALUE):</span><br><span class="line">    sum +&#x3D; i</span><br><span class="line">print(&#39;for循环求和结果为：&#39;+ str(sum))</span><br><span class="line">t2 &#x3D; time.time()</span><br><span class="line">print(&#39;for循环求和开销时间为：&#39;+ str(t2-t1))</span><br></pre></td></tr></table></figure></li>
<li><p>将之前的C代码编译成Python可以读取调用的文件</p>
<ul>
<li>gcc -fPIC -shared C的代码 -o 后续调用文件名.so<br>例如: gcc -fPIC -shared sum.c -o csum.so</li>
</ul>
</li>
<li><p>将csum.so加载到Python中，进行编写</p>
<ul>
<li>导入库文件<code>from ctypes import cdll</code></li>
<li>调用文件  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">result &#x3D; cdll.LoadLibrary(&quot;.&#x2F;csum.so&quot;)</span><br><span class="line">result.add(VALUE)</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>最终Python代码sum.py是酱紫</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">import time</span><br><span class="line">from ctypes import cdll</span><br><span class="line"></span><br><span class="line">VALUE &#x3D; 9999999</span><br><span class="line"></span><br><span class="line"># 使用Python求和</span><br><span class="line">t1 &#x3D; time.time()</span><br><span class="line">sum &#x3D; 0</span><br><span class="line">for i in range(1, VALUE):</span><br><span class="line">    sum +&#x3D; i</span><br><span class="line">print(&#39;for循环求和结果为：&#39;+ str(sum))</span><br><span class="line">t2 &#x3D; time.time()</span><br><span class="line">print(&#39;for循环求和开销时间为：&#39;+ str(t2-t1))</span><br><span class="line">print(&#39;-&#39;*30)</span><br><span class="line"></span><br><span class="line"># Python连接C求和</span><br><span class="line">t1 &#x3D; time.time()</span><br><span class="line">result &#x3D; cdll.LoadLibrary(&quot;.&#x2F;csum.so&quot;)</span><br><span class="line">result.add(VALUE)</span><br><span class="line">t2 &#x3D; time.time()</span><br><span class="line">print(&#39;使用C求和开销时间为：&#39;+ str(t2-t1))</span><br></pre></td></tr></table></figure></li>
<li><p>最终结果</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">for循环求和结果为：49999985000001</span><br><span class="line">for循环求和开销时间为：3.156290292739868</span><br><span class="line">------------------------------</span><br><span class="line">C求和结果为: 49999985000001</span><br><span class="line">使用C求和开销时间为：0.043367624282836914</span><br></pre></td></tr></table></figure>
<h2 id="三、结语"><a href="#三、结语" class="headerlink" title="三、结语"></a>三、结语</h2><p>由上述可以看出来，同样使用for循环，但是使用c的效率是非常非常高的，所以针对一些特殊场景，使用c编写，然后用Python调用也是不错的方法。</p>
</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/05/31/Python%E8%BF%9E%E6%8E%A5C%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%95%88%E7%BC%96%E7%A8%8Bdemo/" data-id="ckaucrn9h000056biakx6ec3p" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/06/01/Python%E9%97%AD%E5%8C%85%E7%9A%84%E7%AE%80%E8%A6%81%E4%BB%8B%E7%BB%8D/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          闭包的简要介绍
        
      </div>
    </a>
  
  
    <a href="/2020/05/30/%E4%BD%BF%E7%94%A8%E9%AB%98%E5%BE%B7%E5%9C%B0%E5%9B%BEAPI%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%B1%9E%E4%BA%8E%E8%87%AA%E5%B7%B1%E7%9A%84%E5%A4%A9%E6%B0%94%E9%97%B9%E9%92%9F/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">使用高德地图API创建一个属于自己的天气闹钟</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">六月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/05/">五月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/06/07/Python%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%93numpy%E7%9A%84%E7%AE%80%E5%8D%95%E5%85%A5%E9%97%A8%E4%BD%BF%E7%94%A8/">Python第三方库numpy的简单入门使用</a>
          </li>
        
          <li>
            <a href="/2020/06/02/Python%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%8F%8A%E7%94%9F%E6%88%90%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/">Python迭代器及生成器的简单理解</a>
          </li>
        
          <li>
            <a href="/2020/06/01/Python%E8%A3%85%E9%A5%B0%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/">Python装饰器的简单理解</a>
          </li>
        
          <li>
            <a href="/2020/06/01/Python%E9%97%AD%E5%8C%85%E7%9A%84%E7%AE%80%E8%A6%81%E4%BB%8B%E7%BB%8D/">闭包的简要介绍</a>
          </li>
        
          <li>
            <a href="/2020/05/31/Python%E8%BF%9E%E6%8E%A5C%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%95%88%E7%BC%96%E7%A8%8Bdemo/">Python连接C实现高效编程demo</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>